#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
    printf("父进程开始执行\n");
    int a=10;
    int status;
    pid_t pid=fork();
    if(pid>0){/*如果是在父进程中执行，则返回的pid是子进程的进程ID(大于0的整数)*/
        a++;
        printf("这是父(%d)进程在执行,a=%d\n", getpid(),a);
        waitpid(pid, &status, 0);
        if (WIFEXITED(status)) {
            printf("子进程正常退出，退出状态: %d\n", WEXITSTATUS(status));
        } else if (WIFSIGNALED(status)) {
            printf("子进程被信号终止，信号编号: %d\n", WTERMSIG(status));
        }
        printf("父进程退出\n");
    }else if(pid==0){
        a-=3;
        printf("这是子(%d)进程在执行,a=%d\n", getpid(),a);
        // char *path = "/bin/ping";
        // char *argv[] = {path,"-c", "6","www.baidu.com", NULL};
        // char *envp[] = {NULL};

        char *path = "/bin/ls";
        char *argv[] = {path,"-l", NULL};
        char *envp[] = {NULL};
    
        // 调用 execve 执行新程序
        int result = execve(path, argv, envp);
    
        if (result == -1) {
            perror("execve ERROR");
            return 1;
        }
        printf("This line should not be printed if execve succeeds.\n");
    }

    return 0;
}